//
//  AlarmStatisticsVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/22.
//
// 【首页-报警通知】报警记录-总分支行

import Foundation
import UIKit

class AlarmStatisticsVC: SubLevelViewController, PullToRefreshPresentable {
    let topV = AlarmStatisticsTopView()
    
    let contentV = UIView()

    var alarmV: AlarmStatisticsAlarmView!
    let armV = AlarmStatisticsArmView()
    
//    var datas : [AlarmsModal?] = []

//    var pageNum: Int = 1
//    var totalPage: Int64 = 0
    
    var param = AlarmCommonParam(dateType: "1")
    var listParam = AlarmCommonParam(pageNum: 1, pageSize: PageSize, dateType: "1")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "报警记录"
        view.backgroundColor = .bg
        NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NotiAlarm, object: nil)

        setupNavItems()
        
        if deptName == nil {
            deptName = BSUser.currentUser.deptName
            deptId = BSUser.currentUser.deptId
        }
        
        listParam.tbCompanyId = "\(deptId ?? -1)"
        param.tbCompanyId = "\(deptId ?? -1)"
        
        alarmV = AlarmStatisticsAlarmView(deptId: deptId, deptName: deptName)
        setupUI()
        
        reloadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navBarStyle(.white)
    }
    
    @objc func segmentSelected(sender: ScrollableSegmentedControl) {
        let index = sender.selectedSegmentIndex
        if index == 0 {
            showAlarm() // 报警信号
        } else if index == 1 {
            showArm() // 撤布防
        }
    }
    
    // 报警信号
    func showAlarm() {
        contentV.removeAllSubViews()

        contentV.addSubview(alarmV)
        alarmV.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalToSuperview()
        }
        
        reloadData()
    }
    
    // 撤布防
    func showArm() {
        contentV.removeAllSubViews()

        contentV.addSubview(armV)
        armV.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalToSuperview()
        }
        
        armV.reloadData(withParam: param)
    }
    
    @objc func chartTapped() {
        navigationController?.pushViewController(AlarmReportVC(withDeptId: deptId, deptName: deptName), animated: true)
    }
    
    @objc func msgTapped() {
        navigationController?.pushViewController(AlarmReplyVC(), animated: true)
    }
    
    @objc func reloadData() {
        /// 撤布防统计
        API.getAlarmCbfCount(withParam: param) { responseModel in
            DispatchQueue.main.async {
                self.topV.summaryV.update(withData: responseModel.model)
            }
        }
        
        alarmV.reloadData(withParam: listParam)
    }
    
    func setupUI() {
        topV.segmentedC.addTarget(self, action: #selector(segmentSelected(sender:)), for: .valueChanged)
        view.addSubview(topV)
        topV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
        }
        
        alarmV.aTable.needReload = { () -> () in
            self.listParam.pageNum = 1
            self.reloadData()
        }
        alarmV.header.filterV.dateCtl.didSelectItemWith = { (index, modal) -> () in
            self.alarmV.header.filterV.dateCtl.key = modal.name
            self.listParam.dateType = modal.id
            self.param.dateType = modal.id
            self.reloadData()
        }
        alarmV.header.filterV.statusCtl.didSelectItemWith = { (index, modal) -> () in
            if modal.id == nil {
                self.alarmV.header.filterV.statusCtl.key = "处置状态"
            } else {
                self.alarmV.header.filterV.statusCtl.key = modal.name
            }
            self.listParam.status = modal.id
            self.reloadData()
        }
        
        view.addSubview(contentV)
        contentV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(topV.snp.bottom)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
        
        showAlarm()
    }
    
    func setupNavItems() {
        let chartBtn = UIButton(type: .custom)
        chartBtn.setImage(UIImage(named: "ic_chart"), for: .normal)
        chartBtn.addTarget(self, action: #selector(chartTapped), for: .touchUpInside)
        chartBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        chartBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
        let chartBar = UIBarButtonItem(customView: chartBtn)
        
//        let searchBtn = UIButton(type: .custom)
//        searchBtn.setImage(UIImage(named: "ic_search"), for: .normal)
//        searchBtn.addTarget(self, action: #selector(searchTapped), for: .touchUpInside)
//        searchBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
//        searchBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
//        let searchBar = UIBarButtonItem(customView: searchBtn)
        
//        let msgBtn = UIButton(type: .custom)
//        msgBtn.setImage(UIImage(named: "ic_alarm_msg"), for: .normal)
//        msgBtn.addTarget(self, action: #selector(msgTapped), for: .touchUpInside)
//        msgBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
//        msgBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
//        let msgBar = UIBarButtonItem(customView: msgBtn)
        
//        navigationItem.rightBarButtonItems = [msgBar, chartBar]
        navigationItem.rightBarButtonItems = [chartBar]
    }
}

